package com.example.ftp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.io.File;
import java.io.IOException;
import java.util.List;

import static com.example.ftp.DebugLog.LOG;

/**
 * Created Date 2019/7/22
 *
 * @author liangkun
 * description：
 */
public class TestUpActivtiy extends AppCompatActivity {
    private static final int REQUEST_PICK_IMAGE = 10086;
    private String realPathFromUri; //相册图片的地址
    private String fileName; //图片名称
    private TextView tvFacePath;
    private Button btnGet;
    private Button btnUp;
    private Button btnDown;
    private Button btnList;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        btnGet = findViewById(R.id.btn_get);
        btnUp = findViewById(R.id.btn_up);
        tvFacePath = findViewById(R.id.tv_path);
        btnDown = findViewById(R.id.btn_down);
        btnList = findViewById(R.id.btn_list);
        new Thread(new Runnable() {
            @Override
            public void run() {
                FTPUtils.getInstance().initFTPSetting("192.168.31.148",9007,"admin","123456");
            }
        }).start();
//        FTPUtils.getInstance().initFTPSetting("ftp://192.168.31.45:9527",9527,"admin","123456");
//
        requestP();
        btnGet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                request();
            }
        });
        btnDown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            FTPUtils.getInstance().downloadSingle();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();

            }
        });
        btnList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            FTPUtils.getInstance().listFiles("ftp://192.168.31.148:9007/");
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();

            }
        });

    }

    private void request() {
        PermissionUtil.requestPermission(this, new PermissionListener() {
            @Override
            public void onGranted() {
                getImage();
                mkdir("FTP_DOWNLOAD");
            }

            @Override
            public void onDenied(List<String> deniedPermissions) {

            }
        });
        btnUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        FTPUtils.getInstance().uploadFile(realPathFromUri,fileName);
                    }
                }).start();

            }
        });
    }
    private void requestP() {
        PermissionUtil.requestPermission(this, new PermissionListener() {
            @Override
            public void onGranted() {
                mkdir("FTP_DOWNLOAD");
            }

            @Override
            public void onDenied(List<String> deniedPermissions) {

            }
        });
        btnUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        FTPUtils.getInstance().uploadFile(realPathFromUri,fileName);
                    }
                }).start();

            }
        });
    }
    /**
     * 获取图片
     */
    private void getImage() {
        Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setType("image/*");
        startActivityForResult(intent, REQUEST_PICK_IMAGE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            switch (requestCode) {
                case REQUEST_PICK_IMAGE:
                    if (data != null) {
                        realPathFromUri = RealPathFromUriUtils.getRealPathFromUri(this, data.getData());
                        fileName = realPathFromUri.substring(realPathFromUri.lastIndexOf("/") + 1, realPathFromUri.length());

                        showText(realPathFromUri);
                        DebugLog.d(LOG + " realPathFromUri " + realPathFromUri);
                    }

                    break;
            }
        }
    }

    private void showText(String realPathFromUri) {
        tvFacePath.setText(realPathFromUri);
    }

    /**
     * @param name 文件名
     * @return 创建成功返回true。否则false。
     */
    public static boolean mkdir(String name) {
        boolean bool = false;

        boolean state = Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED);
        if (state) {
            File f = Environment.getExternalStorageDirectory();
            String path = f.getPath();
            String dir = path + "/" + name + "/";
            File file = new File(dir);
            if (!file.exists()) {
                bool = file.mkdir();
            } else {
                Log.i("", "-----------" + dir + "已存在----------------");
            }

        } else {
            Log.e("", "-----------------外部存储器不可用----------------");
        }

        return bool;
    }
}
